﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using WmWebSystem.Models;
using WmWebSystem.Service;
using WmWebSystem.DbHelper;

namespace WmWebSystem.Controllers
{
    public class SliderController : Controller
    {
        //
        // GET: /Slider/
        WmDb mydb = new WmDb();

        [Authorize(Roles = "admin,editer")]
        public ActionResult Index()
        {
            var img = from i in mydb.Sliders select i;
            return View(img.ToList());
        }

        [Authorize(Roles = "admin,editer")]
        public ActionResult Details(int id)
        {
            Slider slider = mydb.Sliders.Find(id);
            if (slider == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                string image = (from i in mydb.Sliders
                                where i.SliderId == id
                                select i.ImgUrl).FirstOrDefault();
                ViewBag.img = image;
                return View("Details", slider);
            }
        }

        [Authorize(Roles = "admin,editer")]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize(Roles = "admin,editer")]
        [HttpPost]
        public ActionResult SliderCreate()
        {
            string linkto = "";

            if (Request.Form["NewsUrl"] == null)
                linkto = "#";
            else
                linkto = Request.Form["NewsUrl"].ToString();

            string title=Request.Form["ImgTitle"];
            string cont = Request.Form["Brief"];
            string Description = Request.Form["Description"];
            int Type = Convert.ToInt32(Request.Form["projectType"]);
            string TargetValue = Request.Form["TargetValue"];

            string path = null;
            string url = null;
            string fileOldName = "";
            string extension = "";

            Slider slider = new Slider();

            foreach (string image in Request.Files)
            {
                if (!WmWebSystem.Service.HasFiles.HasFile(Request.Files[image]))
                    continue;

                string createPath = AppDomain.CurrentDomain.BaseDirectory + @"Content\Sliders";
                path = HasFolders.HasFolder(createPath);

                //获得文件扩展名
                fileOldName = Path.GetFileName(Request.Files[image].FileName);
                extension = Path.GetExtension(fileOldName);//扩展名 ".xxx"

                string miniType = Request.Files[image].ContentType.ToLower();

                Stream fileStream = Request.Files[image].InputStream;

                //文件重命名
                string fileNewName = RandomNumberTo.RandomNumberToName() + extension;

                Request.Files[image].SaveAs(Path.Combine(path, fileNewName));

                path = path + "\\" + fileNewName;
                url = "~\\Content\\Sliders\\" + fileNewName;

                
                slider.AddTime = DateTime.Now;
                slider.Editor = HttpContext.User.Identity.Name;
                slider.ImgUrl = url;
                slider.DelPath = path;
                slider.Title = title;
                slider.LinkTo = linkto;
                slider.Content = cont;
                slider.Description = Description;
                slider.Type = Type;
                slider.TargetValue = TargetValue;
                
            }
            SliderHelper.CreateNewToSliders(slider);
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "admin,editer")]
        public ActionResult Edit(int id)
        {
            Slider img = mydb.Sliders.Find(id);
            if (img == null)
            {
                return RedirectToAction("Index");
            }
            return View(img);
        }

        [Authorize(Roles = "admin,editer")]
        [HttpPost]
        public ActionResult Edit(Slider slider)
        {
            try
            {
                var Slider = mydb.Sliders.Find(slider.SliderId);
                UpdateModel(Slider);
                mydb.SaveChanges();
                return RedirectToAction("Details", new { id = slider.SliderId });
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "修改失败，请查看详细错误信息:" + e);
            }
            return View(slider);
        }

        [Authorize(Roles = "admin,editer")]
        public ActionResult Delete(int id)
        {
            Slider Slider = mydb.Sliders.Find(id);
            if (Slider == null)
                return RedirectToAction("Index");
            return View(Slider);
        }

        [Authorize(Roles = "admin,editer")]
        [HttpPost]
        public RedirectToRouteResult Delete(int id, FormCollection collection)
        {
            SliderHelper.DeleteFromSliders(id);
            return RedirectToAction("Index");
        }

    }
}
